package com.atguigu.hashtab;

import java.util.Scanner;

public class HashTabDemo {
    public static void main(String[] args) {
        HashTab hashTab = new HashTab(8);
        Scanner scanner = new Scanner(System.in);
        while(true){
            System.out.println("add:  增加雇员");
            System.out.println("list: 遍历雇员");
            System.out.println("find: 查找雇员");
            System.out.println("exit: 结束系统");
            String next = scanner.next();
            switch (next){
                case "add":
                    System.out.println("请输入id:");
                    int id = scanner.nextInt();
                    System.out.println("请输入name:");
                    String name = scanner.next();
                    hashTab.add(new Emp(id,name));
                    break;
                case "list":
                    hashTab.list();
                    break;
                case "find":
                    System.out.println("请输入id:");
                    id = scanner.nextInt();
                    hashTab.findById(id);
                    break;
                case "exit":
                    scanner.close();
                    return;
                default:break;
            }
        }
    }
}

class Emp{
    public Integer id;
    public String name;

    public Emp next;

    public Emp(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}


class EmpLinkedList{
    public Emp head;

    public void add(Emp emp){
        if (head==null) {
            head=emp;
        }else{
            Emp cache=head;
            while(cache.next!=null){
                cache=cache.next;
            }
            cache.next=emp;
        }
    }

    public void list(int i){
        if (head==null) {
            System.out.printf("第%d行数据:zero emp\n",i+1);
        }else{
            Emp cache=head;
            System.out.printf("第%d行数据:",i+1);
            do{
                System.out.print(cache+",");
                cache=cache.next;
            }while(cache!=null);
            System.out.println();
        }
    }

    public Emp findById(int id){
        if(head==null){
            return null;
        }else{
            Emp cache=head;
            do{
                if (cache.id==id) {
                    return cache;
                }
                cache=cache.next;
            }while(cache!=null);
            return null;
        }
    }
}

class HashTab{
    private EmpLinkedList[] empLinkedListArray;

    private Integer arraySize;

    public HashTab(Integer arraySize) {
        this.arraySize = arraySize;
        this.empLinkedListArray=new EmpLinkedList[arraySize];
        for (int i = 0; i < arraySize; i++) {
            empLinkedListArray[i]=new EmpLinkedList();
        }
    }

    public int hashban(int id){
        return id%arraySize;
    }

    public void add(Emp emp){
        empLinkedListArray[hashban(emp.id)].add(emp);
    }

    public void list(){
        for (int i = 0; i < empLinkedListArray.length; i++) {
            EmpLinkedList empLinkedList = empLinkedListArray[i];
            empLinkedList.list(i);
        }
    }

    public void findById(int id){
        Emp emp = empLinkedListArray[hashban(id)].findById(id);
        System.out.println("emp:"+emp);
    }
}